 package ui;

import controller.*;
import model.*;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.InputMismatchException;
public class MenuUI
{
    private SellProductCtr sellCtr;
    private ProductCtr pCtr;
    private CustomerCtr cCtr;
    
    
    
    public MenuUI()
    {
        sellCtr = new SellProductCtr();
        pCtr = new ProductCtr();
        cCtr = new CustomerCtr();
        mainMenuStart();
    }
    
    // MAIN MENU
    
    public void mainMenuStart()
    {
        boolean exit = false;
        while(!exit)
        {
            int choice = writeMainMenu();
            if(choice == 0)
            {
                createCustomer();
            }
            if(choice == 1)
            {
                findCustomer();
            }
            if(choice == 2)
            {
                createProduct();
            }
            if(choice == 3)
            {
                findProduct();
            }
            if(choice == 4)
            {
                findOrder();
            }
            if(choice == 5)
            {
                makeSale();
            }
            if(choice == 6)
            {
                writeEnd();
                exit=true;
            }
            else{
                unknownCommand();
            }
            // ...
        }
        
    }
    
    private int writeMainMenu()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\f ♥♥♥ MainMenu ♥♥♥");
        System.out.println(" (0) Create Customer");
        System.out.println(" (1) Find Customer");
        System.out.println(" (2) Create Product");
        System.out.println(" (3) Find Product");
        System.out.println(" (4) Find Order");
        System.out.println(" (5) Make sale");
        System.out.println(" (6) Quit");
        
        
        
        
        int choice;
        try{
            choice = keyboard.nextInt();
        } catch(InputMismatchException e) {
            choice = -1;
        }
        return choice;
    }
    
    // ACTUAL FUNCTIONS
    
    
    public void createCustomer()
    {
        Scanner keyboard = new Scanner(System.in);
        
        String name, address, phone, groupName;
        
        System.out.println("Enter full name: ");
        name = keyboard.next();
        
        System.out.println("Enter address: ");
        address = keyboard.next();
        
        System.out.println("Enter phone: ");
        phone = keyboard.next();
        
        System.out.println("Enter groupName: ");
        groupName = keyboard.next();
        
        if(cCtr.createCustomer(name, address, phone, groupName))
        {
            System.out.println("Customer " + name + " was created and assigned to "
            + groupName + " group.");
        }
        
        else {
            System.out.println("Customer " + name + " was created. Group not found." +
            " Customer assigned to Default group.");
        }
        
        pause();
    }
    
    public void findCustomer()
    {
        Scanner keyboard = new Scanner(System.in);
        String name;
        
        System.out.println("Enter customer name you are searching for ");
        name = keyboard.next();
        
        ArrayList<Customer> cList = cCtr.findCustomer(name);
        if(cList != null)
        {
            System.out.println("Customer(s) found");
            for(Customer c : cList)
            {
                System.out.println("Id: " + c.getID() + ", Name: " + c.getName() + ", Phone: "
                + c.getPhone() + ", Address: " + c.getAddress() + ", Group Name: " + c.getGroup().getGroupName());
                
                System.out.println("-----------------------------------------------------------------");
            }
        }
        else
        {
            System.out.println("Customer not found");
        }
    }
    
    
    
    public void createProduct()
    {
        Scanner keyboard = new Scanner(System.in);
        
        String barcode, productName, productType, location;
        double price;
        int amount;
        
        System.out.println("Enter barcode: ");
        barcode = keyboard.next();
        
        System.out.println("Enter product name: ");
        productName = keyboard.next();
        
        System.out.println("Enter product type: ");
        productType = keyboard.next();
        
        System.out.println("Enter product location: ");
        location = keyboard.next();
        
        
        System.out.println("Enter price of the product: ");
        try{
            price = keyboard.nextDouble();
        } catch(InputMismatchException e) {
            System.out.println("Wrong format of price. Product creation aborted.");
            price = -1;
        }
        if(price != -1) {
            System.out.println("Enter amount of products: ");
            amount = keyboard.nextInt();
            
            pCtr.createProduct(barcode, productName, productType, price, location, 
                                 amount);
        }
        
        pause();
    }
    
    public boolean findProduct() {
        Scanner keyboard = new Scanner(System.in);
        
        String productName;
        
        System.out.println("Enter name of product you are searching: ");
        productName = keyboard.next();
        
        if(productName == "done")
        {
            return false;
        }
        
        ArrayList<Product> pList = pCtr.findProduct(productName);
        if(pList != null) {
            
            System.out.println("Product found");
            
            for(Product p : pList)
            {
                System.out.println("Name: " + p.getProductName() + ", barcode: " +
                p.getBarcode() + ", type: " + p.getProductType() + ",");
                System.out.println("price: " + p.getPrice() + ", location: " +
                p.getLocation() + " serial: " + p.getSerialNumber() + ", amount: "
                + p.getAmount());
                System.out.println("-----------------------------------------------------------------");
            }
        }
        else {
            System.out.println("Product not found");
        }
        
        pause();
        return true;
    }
    
    
    
    public void findOrder()
    {
        Scanner keyboard = new Scanner(System.in);
        String id;
        
        
        
        findCustomer();
        
        System.out.println("Enter id of customer you want to find order: ");
        id = keyboard.next();
        
        ArrayList<Order> oList = sellCtr.findOrder(id);
        
        if(oList!=null)
        {
            System.out.println("Order found.");
            for(Order o : oList)
            {
                ArrayList<Product> pList = o.getAllProducts();
                System.out.println("Name | Type | Price | Quantity");
                for(Product p : pList)
                {
                    System.out.println(p.getProductName() + " " + p.getProductType() +
                    " " + p.getPrice() + " " + p.getAmount());
                }
                System.out.println("-----------------------------------------------------------------");
            }
        }
        else
        {
            System.out.println("Order not found");
        }
    }
    
    public void makeSale()
    {
        Scanner keyboard = new Scanner(System.in);
        String productName, name, customerId, productId, completed;
        int quantity=0;
        Product p;
        Customer c;
        Order o;
        
        findCustomer();
        
        System.out.println("Enter id of customer");
        customerId = keyboard.next();
        
        c = cCtr.getCustomer(customerId);
        
        if(c != null)
        {
            if(sellCtr.createOrder(c.getID())) {
                System.out.println("Order for " + c.getName() + " was created");
            }
            o = sellCtr.getOrder(customerId);
            
            boolean end = false;
            boolean success = true;
            while(!end)
            {
                
                if(!findProduct())
                {
                    end = true;
                }
                
                 System.out.println("Enter serial number of product to add to order");
                 productId = keyboard.next();
                    // if input is done - cycle ends
                    
               
                
                if(end!=true) {
                    
                    p = pCtr.getProduct(productId);
                
                    System.out.println("Enter amount of product: "); 
                    try{
                        quantity = keyboard.nextInt();
                    }
                    catch(InputMismatchException e)
                    {
                        success = false;
                    }
                
                    if(success)
                    {
                        if(quantity <= p.getAmount())
                        {
                            if(!o.addOrderLine(p, quantity))
                            {
                                System.out.println("We don't have enough of that product");
                            }
                           
                            p.setAmount(p.getAmount() -  quantity);
                            System.out.println(quantity + " of " + p.getProductName() +
                            " has been added to order");
                        }
                        else
                        {
                            System.out.println("We don't have enough of that product");
                        }
                    }
                }
               
                System.out.println("Enter 'done' if you want to complete order");
                completed = keyboard.next();
                if(completed.equals("done")) {
                        end = true;
                }
            }
            System.out.println("------------------------------------------------------------");
            System.out.println();
            System.out.println("Total price: " + sellCtr.getTotal(o.getID()));
            System.out.println("Payment method: " + o.showPaymentType());
            
            
             
            }
            
           
        
        else
        {
            System.out.println("Either product or customer wasn't found");
        }
        
    }
    
    private void unknownCommand() 
    {
        System.out.println("Unknown command");
        pause();
    }
    private void writeEnd()
    {
        System.out.println("System is shutting down");
    }
    private void pause() 
    {
        System.out.println();
        System.out.println("Press Enter to continue");
        Scanner keyboard = new Scanner(System.in);
        String vent = keyboard.nextLine();
    }
    
}